
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Key的原理</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
    <!-- 1.新旧DOM会比较，如果没变化则用旧DOM，有变化就用新dom，所以一般推荐使用数据的唯一标识作为key，特别是带输入框的（input）乱序后容易出问题 -->
    <div id="root">
        
        <ul>
            <h2>人员列表</h2>
            <button @click.once="add">添加一个老刘</button>
            <!-- 遍历数组 -->
            <!-- <li v-for="person in persons" :key="person.id">{{person.name}}-{{person.age}}</li> -->
            <!-- 多个参数最好加个小括号 -->
            <li v-for="(p,index) in persons" :key="p.id" >
                {{p}} --- {{index}} 
                <input type="text"/>
            </li>

        </ul>
    </div>
</body>
<script type="text/javascript">
    Vue.config.productionTip = false

    new Vue({
        el:'#root',
        data:{
            persons:[
                {id:'001',name:'张三 ',age:18},
                {id:'002',name:'李四',age:19},
                {id:'003',name:'王五',age:18}
            ]
        },
        methods: {
            add(){
                const p = {id:'004',name:'老刘',age:40}
                //将新项添加到数组起始位置:
                this.persons.unshift(p)
            }
        },
    })
</script>
</html>